package com.example.practice.adapter;

import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.practice.R;
import com.example.practice.bean.SensorStatus;

public class ControllerSensorAdapter extends BaseAdapter {

	List<SensorStatus> list;
	
	public ControllerSensorAdapter(List<SensorStatus> list) {
		this.list = list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		//填写以下代码
		ViewHolder viewHolder;
		if(arg1 == null){
			arg1 = LayoutInflater.from(arg2.getContext()).inflate(R.layout.controller_grid_item, null);
			viewHolder = new ViewHolder();
			viewHolder.icon = (ImageView) arg1.findViewById(R.id.img_status);
			viewHolder.name = (TextView)arg1.findViewById(R.id.txt_name);
			arg1.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) arg1.getTag();
		}
		
		SensorStatus sensorStatus = list.get(arg0);
		
		viewHolder.name.setText(sensorStatus.name);
		if(sensorStatus.on){
			viewHolder.icon.setImageResource(R.drawable.on);
		}else{
			viewHolder.icon.setImageResource(R.drawable.off);
		}
		return arg1;
	}
	
	private class ViewHolder{
		TextView name;
		ImageView icon;
	}

}
